//
//  LCGlobalLifeViewController.m
//  lechuang
//
//  Created by Mr_hi on 2024/8/27.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCGlobalLifeViewController.h"
#import "LCGolbalLifeCollectionViewCell.h"
#import "LCGlobalService.h"

#import "LCGlobalLifeShareView.h"

@interface LCGlobalLifeViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (assign, nonatomic)NSInteger page;
@property (strong, nonatomic)NSMutableArray * dataArr;

@end

@implementation LCGlobalLifeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self setContentViewRefresh];
    [self getLifeData:1];
}

-(void)setContentViewRefresh {
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getLifeData:1];
    }];
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getLifeData:self.page];
    }];
    self.collectionView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

- (void)getLifeData:(NSInteger)page {
    [LCGlobalService getLifeListWithPage:page Block:^(BOOL isSuccess, NSArray<LCGlobalLifeModel *> *list, NSInteger total) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.collectionView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 17, 14, 17);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cellW =ceilf(ScreenW-34);
    return CGSizeMake(cellW, 95);
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGolbalLifeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCGolbalLifeCollectionViewCell" forIndexPath:indexPath];
    LCGlobalLifeModel * life = self.dataArr[indexPath.item];
    cell.life = life;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGlobalLifeModel * life = self.dataArr[indexPath.item];
    
    LCGlobalLifeShareView * view = [[LCGlobalLifeShareView alloc] initForNibWithKey:life.comandkey];
    [view show];
}

@end
